package ca.gc.crc.demoapp;

import ca.gc.crc.libfmrds.FMinterface;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity implements View.OnClickListener {
	
	private String TAG = "demoApp";
	private FMinterface CRC_FMRDS_Library = new FMinterface();
	private AudioManager aManager;
	private int currentFreq = 106100; //in kHz
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
		// Connect button to callback listener
		((Button)findViewById(R.id.ButtonStart)).setOnClickListener(this);
    }
    
    @Override
    public void onStart(){
    	super.onStart();
    	aManager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE); //Put this in the doc? 	
    }
    
    /**
	 * Called when a button from the main player view is pressed.
	 * 
	 * @param v the clicked button
	 */
    @Override
	public void onClick(View v) {
		switch (v.getId())
		{
		case R.id.ButtonStart:
			String tmp = ((EditText)findViewById(R.id.EditText01)).getText().toString();
			currentFreq = Integer.parseInt(tmp.trim());
			
			if (CRC_FMRDS_Library.radioIsSupported()) {
	    		Log.d(TAG, "-------------->> FM-RDS Radio receiver detected on this phone!");
	    		
	    		Log.d(TAG, "-------------->> Opening FM-RDS receiver...");
	    		CRC_FMRDS_Library.openRadio(aManager);
	    		
	    		Log.d(TAG, "-------------->> Configuring FM-RDS receiver...");
	        	CRC_FMRDS_Library.setBand(CRC_FMRDS_Library.BAND_87500_108000_kHz);
	        	CRC_FMRDS_Library.setSpacing(CRC_FMRDS_Library.CHAN_SPACING_100_kHz);
	        	CRC_FMRDS_Library.setEmphasis(CRC_FMRDS_Library.DE_TIME_CONSTANT_75);
	        	
	        	Log.d(TAG, "-------------->> Setting the frequency at "+currentFreq+" kHz");
	        	CRC_FMRDS_Library.setChannel(currentFreq);
	        	
	        	Log.d(TAG, "-------------->> Receiving and decoding loop: processing RDS for 5 seconds...");
	        	int i = 0;
	        	while (i < 20){
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						Log.d(TAG, "----------------> Exception in sleep: " + e.getMessage());
					}
					CRC_FMRDS_Library.processRDS();
					CRC_FMRDS_Library.syncMediaVolume(aManager);
					i++;
	        	}
	        	
	        	Log.d(TAG, "-------------->> Updating the GUI with received RDS...");
	        	String PI = Integer.toHexString(CRC_FMRDS_Library.getPI());
	        	String PS = CRC_FMRDS_Library.getPS();
	        	String RT = CRC_FMRDS_Library.getRT();
	        	((TextView)findViewById(R.id.TextView02)).setText(PI);
	        	((TextView)findViewById(R.id.TextView04)).setText(PS);
	        	((TextView)findViewById(R.id.TextView06)).setText(RT);
	        	
	        	Log.d(TAG, "-------------->> Closing FM-RDS receiver...");
	        	CRC_FMRDS_Library.closeRadio(aManager);
	    	} else {
	    		Toast.makeText(this.getApplicationContext(), "This phone is not supported by the CRC FM-RDS Library for Android", 1);
	    	}
			break;
		default:
			break;
		}
    }
    
}